<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>扫码登录</title>
    <style>
        #loginButton {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<h1>扫码登录</h1>
<button id="loginButton">点击扫码登录</button>

<script>
    // 打开扫码登录页面的弹出窗口
    document.getElementById('loginButton').addEventListener('click', function() {
        // 假设扫码页面会重定向到以下 URL，并附加 token 参数
        let loginUrl = 'https://channels.weixin.qq.com/login.html';
        let loginWindow = window.open(loginUrl, '扫码登录', 'width=500,height=600');

        // 每隔一段时间检查窗口是否关闭
        let checkWindow = setInterval(() => {
            if (loginWindow.closed) {
                clearInterval(checkWindow);
                // 窗口关闭后，获取当前 URL 的查询参数
                let token = new URLSearchParams(window.location.search).get('token');
                if (token) {
                    console.log('扫码登录成功，令牌:', token);
                    alert('扫码登录成功，令牌:' + token);
                } else {
                    alert('未获取到令牌');
                }
            }
        }, 1000);
    });

    // 监听父页面地址的变化，获取 URL 查询参数
    window.addEventListener('popstate', function() {
        const urlParams = new URLSearchParams(window.location.search);
        const token = urlParams.get('token');
        if (token) {
            console.log('获取到令牌:', token);
            alert('获取到令牌: ' + token);
        }
    });
</script>
</body>
</html>
